package cn.edu.zsc.blms.service.impl.array;

import cn.edu.zsc.blms.App;
import cn.edu.zsc.blms.domain.BaseEntity;
import cn.edu.zsc.blms.domain.Reader;
import cn.edu.zsc.blms.exception.EntityNotExistException;
import com.sun.xml.internal.bind.v2.runtime.output.StAXExStreamWriterOutput;

public abstract class BaseServiceImpl {

    protected final BaseEntity[] DATA;
    protected int idCounter;

    public BaseServiceImpl() {
        DATA = new BaseEntity[100];
    }

    /**
     * 获取用户输入的有效ID值，返回-1表示用户输入的ID值是无效的...
     */
//    protected int getInputId(){
//        if(!App.input.hasNextInt()){
//            App.input.nextLine();
//            System.out.println("ID为整数，请输入整数值");
//            return -1;
//        }
//        int id = App.input.nextInt();
//        App.input.nextLine();
//        if(id>=idCounter || DATA[id]==null){
//            System.out.println("不存在该ID的数据");
//            return -1;
//        }
//        return id;
//    }
//}
    protected BaseEntity getOne() throws EntityNotExistException {
        BaseEntity entity;
        try {
            int id = Integer.parseInt(App.input.nextLine());
            if (id >= idCounter || DATA[id] == null) {
                throw new EntityNotExistException();
            }
            entity = DATA[id];
        } catch (NumberFormatException e) {
            throw new EntityNotExistException("ID为整数，请输入整数值");
        }
        return entity;
    }


}
